package com.lcp.controller;

import com.lcp.common.Result;
import com.lcp.entity.ConsultationRecord;
import com.lcp.entity.User;
import com.lcp.service.ConsultationRecordService;
import com.lcp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/consultation-records")
public class ConsultationRecordController {

    @Autowired
    private ConsultationRecordService consultationRecordService;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public Result<ConsultationRecord> getById(@PathVariable Long id) {
        return Result.success(consultationRecordService.getById(id));
    }

    @GetMapping("/current")
    public Result<List<ConsultationRecord>> getCurrentUserRecords() {
        User currentUser = userService.getCurrentUser();
        return Result.success(consultationRecordService.listByUserId(currentUser.getId()));
    }

    @PostMapping
    public Result<ConsultationRecord> create(@RequestBody ConsultationRecord record) {
        User currentUser = userService.getCurrentUser();
        record.setUserId(currentUser.getId());
        return Result.success(consultationRecordService.create(record));
    }

    @PutMapping("/{id}")
    public Result<ConsultationRecord> update(@PathVariable Long id, @RequestBody ConsultationRecord record) {
        record.setId(id);
        return Result.success(consultationRecordService.update(record));
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        consultationRecordService.delete(id);
        return Result.success();
    }

    @GetMapping("/date-range")
    public Result<List<ConsultationRecord>> listByDateRange(
            @RequestParam String startDate,
            @RequestParam String endDate) {
        User currentUser = userService.getCurrentUser();
        return Result.success(
            consultationRecordService.listByDateRange(currentUser.getId(), startDate, endDate)
        );
    }
} 